﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace DomainDriver.DomainModeling.TransactionScripts
{
    public class DelegateTransactionScript<RETURN_TYPE, ARG_TYPE> : TransactionScriptBase<RETURN_TYPE>
    {
        private ARG_TYPE m_Argument;
        private Func<ARG_TYPE, RETURN_TYPE> m_Delegate;

        public DelegateTransactionScript()
        {
            m_Argument = default(ARG_TYPE);
            m_Delegate = null;
        }

        public ARG_TYPE Argument
        {
            get { return m_Argument; }
            set
            {
                if (HasRun)
                { throw new ApplicationException("Cannot set argument after script was run."); }
                m_Argument = value;
            }
        }

        public Func<ARG_TYPE, RETURN_TYPE> Delegate
        {
            get { return m_Delegate; }
            set
            {
                if (HasRun)
                { throw new ApplicationException("Cannot set delegate after script was run."); }
                m_Delegate = value;
            }
        }

        public override bool IsReady()
        {
            if (m_Argument == null)
            { return false; }
            if (m_Delegate == null)
            { return false; }
            return base.IsReady();
        }

        protected override RETURN_TYPE RunScriptLogicWithResult()
        {
            RETURN_TYPE result = m_Delegate(m_Argument);
            return result;
        }
    }
}